<template>
  <!-- 添加或修改口岸信息管理对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right"  class="flex-form" size="small">
          <el-form-item label="口岸代码" prop="portCode">
        <el-input v-model.trim="form.portCode" placeholder="请输入口岸代码" />
      </el-form-item>

          <el-form-item label="中文名称" prop="portEnName">
        <el-input v-model.trim="form.portEnName" placeholder="请输入中文名称" />
      </el-form-item>

          <el-form-item label="英文名称" prop="portCnName">
        <el-input v-model.trim="form.portCnName" placeholder="请输入英文名称" />
      </el-form-item>

          <el-form-item label="关区代码" prop="customsCode">
        <el-select v-model.trim="form.customsCode" placeholder="请选择关区代码">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>

          <el-form-item label="省份" prop="province">
        <el-input v-model.trim="form.province" placeholder="请输入省份" />
      </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
  </el-dialog>
</template>

<script>
import { getPortCodeInfo, addPortCodeInfo, updatePortCodeInfo } from "@/api/swdecl/portCodeInfo";
export default {
  name: "PortCodeInfo-addEdit",
  props: {
    dialogType: {
      type: String,
      default: "add",
    },
    rowData: {
      type: Object,
      default: () => {},
    },
    dict:{}
  },
  data() {
    return {
      // 弹出层
      dialogVisible: false,
      // 标题
      title: "新增",
      // 表单参数
      form: {
                                              portCode: null,
                                                            portEnName: null,
                                                            portCnName: null,
                                                            customsCode: null,
                                                            province: null

      },
      // 口岸信息管理表格数据
      portCodeInfoList: [],
      // 表单校验
      rules: {
        portCode: [
          { required: true, message: "口岸代码不能为空", trigger: "blur" }
        ],
        portEnName: [
          { required: true, message: "中文名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        if (this.dialogType == "add") {
          this.title = "新增";
        } else if (this.dialogType == "edit") {
          this.title = "编辑";
          this.loadDetail();
        } else if (this.dialogType == "detail") {
          this.title = "详情";
          this.loadDetail();
        }
      });
    },
    close() {
      this.$refs["form"].resetFields();
      this.dialogVisible = false;
    },

    // 获取详情
    loadDetail() {
      const portCode = this.rowData.portCode;
      getPortCodeInfo(portCode).then((res) => {
        if (res.code == 200) {
          this.form = res.data;
                  }
      });
    },
    // 提交表单
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = {
            ...this.form,
          };
          if (this.dialogType == "add") {
            addPortCodeInfo(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          } else {
            params.id = this.rowData.id;
            updatePortCodeInfo(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
